package com.example.day1.infoList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.day1.R;

import java.util.List;

public class InfoAdapter extends BaseAdapter {
    Context context;
    List<Info>infos;

    public InfoAdapter(Context context, List<Info> infos) {
        this.context = context;
        this.infos = infos;
    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_info,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        Info info =infos.get(position);
        holder.imageView.setImageResource(info.getImgId());
        holder.tv_title.setText(info.getTitle());
        holder.tv_describe.setText(info.getDescribe());
        return convertView;
    }
    class ViewHolder{
        ImageView imageView;
        TextView tv_title;
        TextView tv_describe;
        public ViewHolder(View view){
            imageView=view.findViewById(R.id.item_info_iv);
            tv_title=view.findViewById(R.id.item_info_tv);
            tv_describe=view.findViewById(R.id.item_info_describe);
        }
    }
}